package com.lotter.api.controller;


import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lotter.api.service.BankCardService;
import com.lotter.api.service.OnlineUserService;
import com.lotter.api.service.UserAccountService;
import com.lotter.common.enumobject.UserAccountApplyEunm;
import com.lotter.common.enumobject.UserAccountTypeEunm;
import com.lotter.common.exception.LotterCusException;
import com.lotter.common.util.MD5Util;
import com.lotter.common.util.StringUtil;
import com.lotter.invocation.StaticDataContext;
import com.lotter.model.bankcard.BankCard;
import com.lotter.model.user.UserAccount;

/**
 * @ClassName: UserAccountController 
 * @Description: 前台会员信息   设置功能
 * @author zhangzechi
 * @date 2016年7月27日 上午11:25:30  
 * @version V1.0
 */
@Controller
@RequestMapping(value="/website/user")
public class UserAccountController extends BaseController {

	
	@Autowired
	private UserAccountService userAccountService ;
	
	
	@Autowired
	private BankCardService bankCardService;
	
	@Autowired
	private OnlineUserService onlineUserService;
	
	/**
	 * 申请房主
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/houserApply",method = RequestMethod.GET)
	public Map<String,Object> houserApply(@RequestParam(value ="token",required = true) String token) {
		if(StringUtil.isBlank(token)){
			return ajaxLotterStage("提供信息不全，token未提交");
		}
		//1 是申请
		try {
			Long uid = StaticDataContext.onLineUid(token);
			UserAccount userAccount = userAccountService.selectByPrimaryKey(uid);
			if(UserAccountTypeEunm.PUBLISHER.getCode() == userAccount.getUtype())
				return ajaxLotterUser("已经是房主了,不需要申请");
			
			if(UserAccountApplyEunm.APPLY_HOURSE.getCode() == userAccount.getApplyhourse())
				return ajaxLotterUser("已经申请过了,重复申请");
			
			userAccountService.updateApplyhourse(userAccount.getId());
		} catch (Exception e) {
			logger.error("申请房主失败");
			return ajaxLotterServer("申请房主失败,请联系管理员");
		}
		return ajaxLotterSuccess();
	}

	
	/**
	* @Title: resetPwd 
	* @Description: 修改密码
	* @param @param request
	* @param @param password
	* @param @param newpassword
	* @param @param rePassword
	* @param @return    设定文件 
	* @return Map<String,Object>    返回类型 
	* @throws
	 */
	@ResponseBody
	@RequestMapping(value="/resetPwd",method = RequestMethod.POST)
	public Map<String, Object> resetPwd(@RequestParam(value ="token",required = true) String token,
			@RequestParam(value="password",required=true) String password,
    		@RequestParam(value="newpassword",required=true) String newpassword,
    		@RequestParam(value="rePassword",required=true) String rePassword) {
		
		if(StringUtil.isBlank(token)){
			return ajaxLotterStage("token 不能为空");
		}
		
		if(StringUtil.isBlank(password))
			return ajaxLotterStage("原密码不能为空");
		
		if(StringUtil.isBlank(newpassword) || StringUtil.isBlank(rePassword) )
			return ajaxLotterStage("新密码不能为空");
		
		if(password.length() < 6 )
			return ajaxLotterStage("原密码必须输入6位及以上字符");
		
		if(newpassword.length() < 6 )
			return ajaxLotterStage("新密码必须输入6位及以上字符");
		
		if(!newpassword.equals(rePassword))
			return ajaxLotterStage("两次输入密码不相同");
	
		try {
			Long uid = StaticDataContext.onLineUid(token);
			UserAccount userAccount = userAccountService.selectByPrimaryKey(uid);
			
			if(!MD5Util.md5Hex(password).equals(userAccount.getPassword())){
			    return ajaxLotterUser("原密码输入错误");
			}
			
			Boolean bool = bankCardService.findBankCardByUid(uid);
			if(!bool){
				return ajaxLotterUser("你还没有绑定过银行卡,请重新操作");
			}
			
			userAccountService.updatePwdByID(  newpassword,userAccount.getId());    
		} catch (Exception e) {
			logger.error("修改密码出错，请查看信息");
			return ajaxLotterServer("修改密码出错,请联系管理员");
		}
		return ajaxLotterSuccess();
	}
	

	/**
	 * 更新支付密码
	 * @param request
	 * @param oldPaypwd
	 * @param newPaypwd
	 * @param rePaypwd
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/updatePayPwd",method = RequestMethod.POST)
	public Map<String,Object> updatePayPwd(@RequestParam(value ="token",required = true) String token,
			@RequestParam(value="oldPaypwd",required=true) String oldPaypwd,
    		@RequestParam(value="newPaypwd",required=true) String newPaypwd,
    		@RequestParam(value="rePaypwd",required=true) String rePaypwd) {
		
		if(StringUtil.isBlank(token)){
			return ajaxLotterStage("token 不能为空");
		}
		
		if(StringUtil.isBlank(oldPaypwd))
			return ajaxLotterStage("原支付密码不能为空");
		
		if(StringUtil.isBlank(newPaypwd) || StringUtil.isBlank(newPaypwd) )
			return ajaxLotterStage("新支付密码不能为空");
		
		if(oldPaypwd.length() < 6 )
			return ajaxLotterStage("原支付密码必须输入6位及以上字符");
		
		if(newPaypwd.length() < 6 )
			return ajaxLotterStage("新支付密码必须输入6位及以上字符");
		
		if(!newPaypwd.equals(rePaypwd))
			return ajaxLotterStage("两次输入支付密码不相同");
		
		//用户信息
		try {
			Long uid = StaticDataContext.onLineUid(token);
			UserAccount userAccount = userAccountService.selectByPrimaryKey(uid);
			if(MD5Util.md5Hex(newPaypwd).equals(userAccount.getPassword())){
				return ajaxLotterUser("支付密码不能和登录登录相同");
			}
			BankCard bankCard = bankCardService.findBank(userAccount.getId());
			if(bankCard ==null){
				return ajaxLotterUser("你还没有绑定过银行卡，请重新操作");
			}
			if(!MD5Util.md5Hex(oldPaypwd).equals(bankCard.getPaypwd())){
				return ajaxLotterUser("原输入密码不正确");
			}
			bankCardService.updateParpwd(bankCard.getId(),newPaypwd);   
			
		} catch (Exception e1) {
			logger.error("更新支付密码出错");
			return ajaxLotterServer("服务器异常,请联系管理员");
		}
		return ajaxLotterSuccess();
	}
	
	
	/**
	 * 查询用户信息
	 * @param token
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/userinfo",method = RequestMethod.GET)
	public Map<String,Object> userinfo(@RequestParam(value ="token",required = true) String token){
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			Long uid = StaticDataContext.onLineUid(token);
			UserAccount userAccount = userAccountService.selectByPrimaryKey(uid);
			result.put("userAccount", userAccount);
		} catch (LotterCusException e) {
			logger.error("更新图像出错,请检查代码");
			return ajaxLotterServer("服务器异常，请联系管理员");
		}
		return ajaxLotterSuccess(result);
    }
	/**
	 * 更新头像 昵称
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/updateImgInfo",method = RequestMethod.POST)
	public Map<String,Object> updateImgInfo(@RequestParam(value ="token",required = true) String token,
			@RequestParam(value="nickname",required=true) String nickname,
			@RequestParam(value="headurl",required=true) String headurl) {

		if(StringUtil.isBlank(nickname))
			return ajaxLotterStage("昵称不能为空");
		if(StringUtil.isBlank(headurl))
			return ajaxLotterStage("图像地址不能为空");
		try {
			Long uid = StaticDataContext.onLineUid(token);
			userAccountService.updateImgInfo(uid, nickname, headurl);
		} catch (LotterCusException e) {
			logger.error("更新图像出错,请检查代码");
			return ajaxLotterServer("服务器异常，请联系管理员");
		}
		return ajaxLotterSuccess();
	}
	
	
	/**
	 * 退出
	 * @param token
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/logout",method = RequestMethod.GET)
	public Map<String,Object> logout(@RequestParam(value ="token",required = true) String token){
		try {
			Long uid = StaticDataContext.onLineUid(token);
			onlineUserService.logout(uid);
		} catch (LotterCusException e) {
			logger.error("退出有误");
			return ajaxLotterServer("服务器异常，请联系管理员");
		}
		return ajaxLotterSuccess();
	}
	
}
